// pages/addres_list/addres_list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addresList:[],
    morenStste:0,
    states: 0,
    index:0,
    addresIndex:0,
    img:"http://qiniu.keleziyouxing.com//applet_ico_"
  },
//  默认地址
  moren:function(e){
    var index = e.currentTarget.id
    var indexv = 'addresList['+index+'].state'
    var state = this.data.addresList[index].state
    var list = this.data.addresList
    this.setData({addresIndex:index})
    // if(state == 0){
    //   this.setData({
    //     [indexv]: 1,
    //   })
    // }else{
    //   this.setData({
    //     [indexv]: 0,
    //   })
    // } 
    wx.setStorageSync('addresList', this.data.addresList)
  },
  // 删除
  del: function (e) { var index = e.currentTarget.id; var list = this.data.addresList; list.splice(index,1); 
    wx.setStorageSync('addresList', list)
  this.setData({addresList:list});
 },
  // 修改
  editor:function(e){
    var index = e.currentTarget.id;
    var list = this.data.addresList; 
    wx.navigateTo({
      url: '../editor_addres/editor_addres?state=1&index='+index,
    })
    this.setData({
      index:index
    })
  },
  // 添加地址
  add:function(){
    wx.navigateTo({
      url: '../editor_addres/editor_addres?state=0&index=""',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

      var list =[]
    if (!wx.getStorageSync('addresList')){
        list = this.data.addresList
      }else{
        list = wx.getStorageSync('addresList')
        console.log(this.data.list)
      }
     this.setData({
       addresList:list
     })
  },
  addres:function(data,state){
    console.log(state)
    var list1 = []
    if(state == 1){//修改
        console.log(this.data.index)
      var list = wx.getStorageSync('addresList')
      list.splice(this.data.index,1,data)
      wx.setStorageSync('addresList', list)
      this.setData({
        addresList: list
      })
      console.log(wx.getStorageSync('addresList'))
    }
    if(state == 0){//新增
      if (!wx.getStorageSync('addresList')) {
        list1 = []
      } else {
        list1 = wx.getStorageSync('addresList')
      }
      list1.push(data)
      wx.setStorageSync('addresList', list1)
      this.setData({
        addresList:list1
      })
    }
  },
  // 返回
  back:function(e){
    var index = e.currentTarget.id
    var page = getCurrentPages()
    if(page.length>1){
        // 当前页
        var currPage = page[page.length-1]
        // 上一页
        var pervePage = page[page.length -2]
      pervePage.setData({ state: 1, detail:this.data.addresList[index]})
    } 
    wx.navigateBack({
      delta:1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
